package com.qf.fmw.controller;

import com.qf.fmw.entity.vo.RespResult;
import com.qf.fmw.service.AddressService;
import com.qf.fmw.service.impl.AddressServiceImpl;
import com.qf.fmw.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddressController")
public class AddressController extends BaseServlet {
    private AddressService addressService = new AddressServiceImpl();

    // 获取所有地址
    protected RespResult getAllAddresses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return addressService.showAllAddresses();
    }

    // 根据名字和电话搜索地址
    protected RespResult searchAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String query = request.getParameter("query");
        return addressService.searchAddress(query);
    }

    // 删除地址
    protected RespResult deleteAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int addressId = Integer.parseInt(request.getParameter("addressId"));
        return addressService.deleteAddress(addressId);
    }

    // 添加地址
    protected RespResult addAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        int state = Integer.parseInt(request.getParameter("state"));
        return addressService.addAddress(name, phone, detail, state);
    }

    // 修改地址
    protected RespResult updateAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int addressId = Integer.parseInt(request.getParameter("addressId"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        int state = Integer.parseInt(request.getParameter("state"));
        return addressService.updateAddress(addressId, name, phone, detail, state);
    }
}
